// pages/backMessage/backMessage.js

const APP = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    pageNum: 1,
    pageSize: 3,
    navList: [],
    temp: [],
    temp1: [],
    dateStart: '',
    dateend: '2020-12-08',
    time: "09:00",
    times: "09:00",
    index: 0,
    array: [30, 60],
    indexMornS: 0,
    indexMorE: 0,
    indexAftS: 0,
    indexAftE: 0,

    areaList: {
      province_list: {
        // //省
      },
      city_list: {
        // // 市
      },
      county_list: {
        // // 区
      }
    },
    code1: '',
    // 140000
    code2: '',
    code3: '',
    value1: '',
    value2: '',
    value3: '',
    value5: '',
    value6: '',
    value7: '',
    title: '',
    show5: false,
  },
  //监听input事件
  deptName: function (e) {

    this.setData({
      ['form.deptName']: e.detail.value

    });
  },
  phone: function (e) {

    this.setData({
      ['form.phone']: e.detail.value
    });

  },
  address: function (e) {

    this.setData({
      ['form.address']: e.detail.value
    });

  },
  rooms: function (e) {

    this.setData({
      ['form.rooms']: e.detail.value
    });

  },
  remark: function (e) {
    console.log(e.detail)
    this.setData({
      ['form.remark']: e.detail
    });

  },
  // 请求 省数据  请求成功打开省弹窗
  showPopup1() {
    wx.removeStorage({
      key: 'peopleOptions',
    })
    this.setData({
      peopleOptions: [],
      'areaList.province_list': {},
    })
    var postObj = {
      url: '/mobileUser/subscribe/queryProvince',
      method: 'get',
      data: ''
    }

    APP.request.backWxRequest(postObj).then(res => {
      console.log('省', res.data.data)
      this.setData({
        options1: res.data.data
      })

      var options1 = this.data.options1

      var objList = {}
      options1.forEach((items, i) => {
        var obj = APP.transformObject(items.areaId, items.name)
        objList = {
          ...objList,
          ...obj
        }
      })
      this.setData({
        show1: true,
        show2: false,
        show3: false,
        'areaList.province_list': objList,
        //同时要把市 区的数据清空
        // value1: '',
        // value2: '',
        // value3: '',
        // options2: [], // 市
        // options3: [], // 区
        // // 'areaList.province_list': {},
        // 'areaList.city_list': {},
        // 'areaList.county_list': {},
        deptId: '',
        // jianyuList: [],
        jianyuActiveIndex: -1,
        prisons: [],
        deptId: '',
        daySelecter: [],
        timeRangeSelecter: [],
        'yuyueForm.deptId': ''
      })
    })


  },

  onClick: function () {
    this.setData({
      pageNum: 1,
      pageSize: 4,
      navList: []
    })
    this.getMessageList();
  },
  onChange(e) {

    this.setData({
      paramas: {
        deptName: e.detail,
      }
    });
  },
  onSearch() {
    this.setData({
      pageNum: 1,
      pageSize: 4,
      navList: []
    })
    this.getMessageList();
  },
  // 关闭省市区弹窗
  onClose() {
    this.setData({
      show1: false,
      show2: false,
      show3: false,
      showTime: false,
      show5: false,
      showChooseDialog: false,
      showChoosePrison: false,
    });
  },
  cancel() {
    this.setData({
      paramas: '',
      show: false
    })
  },
  // 选择事件
  // 确认 省  点击省的确认  请求市
  confirm1(e) {
    this.setData({
      pageNum: 1,
      pageSize: 4,
      navList: []
    })
    this.onClose()
    this.getMessageList();
  },
  cancel1(e) {
    console.log(e);
    this.onClose()
  },
  //省市区监听值改变
  change1(e) {
    // console.log(e);
    var values = e.detail.values
    var name1 = e.detail.values[0].name
    var areaId1 = e.detail.values[0].code
    console.log('省areaId', areaId1)
    // 省areaId 存在  请求市接口
    if (!!areaId1) {
      //  选择省确认时候同时选择市  市接口请求
      var postObj = {
        url: '/mobileUser/subscribe/queryCity',
        method: 'get',
        data: {
          areaId: areaId1
        }
      }
      APP.request.backWxRequest(postObj).then(res => {
        console.log('市', res.data.data)

        this.setData({
          options2: res.data.data
        })
        var options2 = this.data.options2
        var objList2 = {}
        options2.forEach((items, i) => {
          var obj = APP.transformObject(items.areaId, items.name)
          objList2 = {
            ...objList2,
            ...obj
          }
        })
        this.setData({
          'areaList.city_list': objList2,
        })
      })
    }

    var name2 = ''
    var areaId2 = ''
    if (!!e.detail.values[1]) {
      name2 = e.detail.values[1].name
      areaId2 = e.detail.values[1].code
      console.log('市areaId', areaId2)
    }
    // 市areaId 存在  请求区接口
    if (!!areaId2) {
      //  选择市确认时候同时选择区  区接口请求
      var postObj = {
        url: '/mobileUser/subscribe/queryArea',
        method: 'get',
        data: {
          areaId: areaId2
        }
      }
      APP.request.backWxRequest(postObj).then(res => {
        // console.log('区', res.data.data)
        this.setData({
          options3: res.data.data
        })
        var options3 = this.data.options3
        var objList3 = {}
        options3.forEach((items, i) => {
          var obj = APP.transformObject(items.areaId, items.name)
          objList3 = {
            ...objList3,
            ...obj
          }
        })
        this.setData({
          'areaList.county_list': objList3,
        })
      })
    }

    var name3 = ''
    var areaId3 = ''
    if (!!e.detail.values[2]) {
      name3 = e.detail.values[2].name
      areaId3 = e.detail.values[2].code
      // console.log('市areaId', areaId3)
    }

    // 省市区赋值
    this.setData({
      value1: name1,
      value2: name2,
      value3: name3,
      paramas: {
        province: areaId1,
        city: areaId2,
        area: areaId3
      }
    })

  },
  showPopup2() {
    wx.removeStorage({
      key: 'peopleOptions',
    })
    this.setData({
      peopleOptions: [],
      'areaList1.province_list': {},
    })
    var postObj = {
      url: '/mobileUser/subscribe/queryProvince',
      method: 'get',
      data: ''
    }

    APP.request.backWxRequest(postObj).then(res => {
      console.log('省', res.data.data)
      this.setData({
        options1: res.data.data
      })

      var options1 = this.data.options1

      var objList = {}
      options1.forEach((items, i) => {
        var obj = APP.transformObject(items.areaId, items.name)
        objList = {
          ...objList,
          ...obj
        }
      })
      this.setData({

        show: false,
        show5: true,
        show2: false,
        show3: false,
        'areaList1.province_list': objList,
        //同时要把市 区的数据清空
        // value1: '',
        // value2: '',
        // value3: '',
        // options2: [], // 市
        // options3: [], // 区
        // // 'areaList.province_list': {},
        // 'areaList.city_list': {},
        // 'areaList.county_list': {},
        deptId: '',
        // jianyuList: [],
        jianyuActiveIndex: -1,
        prisons: [],
        deptId: '',
        daySelecter: [],
        timeRangeSelecter: [],
        'yuyueForm.deptId': ''
      })

    })

  },
  confirm2(e) {
    this.setData({
      show: true,
      pageNum: 1,
      pageSize: 4,
      // navList: []
    })
    this.onClose()

  },
  cancel2(e) {
    this.setData({

    });
    console.log(e);
    this.onClose()
  },
  //省市区监听值改变
  change2(e) {
    // console.log(e);
    var values = e.detail.values
    var name1 = e.detail.values[0].name
    var areaId1 = e.detail.values[0].code
    console.log('省areaId', areaId1)
    // 省areaId 存在  请求市接口
    if (!!areaId1) {
      //  选择省确认时候同时选择市  市接口请求
      var postObj = {
        url: '/mobileUser/subscribe/queryCity',
        method: 'get',
        data: {
          areaId: areaId1
        }
      }
      APP.request.backWxRequest(postObj).then(res => {
        console.log('市', res.data.data)

        this.setData({
          options2: res.data.data
        })
        var options2 = this.data.options2
        var objList2 = {}
        options2.forEach((items, i) => {
          var obj = APP.transformObject(items.areaId, items.name)
          objList2 = {
            ...objList2,
            ...obj
          }
        })
        this.setData({
          'areaList1.city_list': objList2,
        })
      })
    }

    var name2 = ''
    var areaId2 = ''
    if (!!e.detail.values[1]) {
      name2 = e.detail.values[1].name
      areaId2 = e.detail.values[1].code
      console.log('市areaId', areaId2)
    }
    // 市areaId 存在  请求区接口
    if (!!areaId2) {
      //  选择市确认时候同时选择区  区接口请求
      var postObj = {
        url: '/mobileUser/subscribe/queryArea',
        method: 'get',
        data: {
          areaId: areaId2
        }
      }
      APP.request.backWxRequest(postObj).then(res => {
        // console.log('区', res.data.data)
        this.setData({
          options3: res.data.data
        })
        var options3 = this.data.options3
        var objList3 = {}
        options3.forEach((items, i) => {
          var obj = APP.transformObject(items.areaId, items.name)
          objList3 = {
            ...objList3,
            ...obj
          }
        })
        this.setData({
          'areaList1.county_list': objList3,
        })
      })
    }

    var name3 = ''
    var areaId3 = ''
    if (!!e.detail.values[2]) {
      name3 = e.detail.values[2].name
      areaId3 = e.detail.values[2].code
      // console.log('市areaId', areaId3)
    }

    // 省市区赋值
    this.setData({
      value5: name1,
      value6: name2,
      value7: name3,
      ['form.province']: areaId1,
      ['form.city']: areaId2,
      ['form.area']: areaId3,
      //   paramas: {
      //     province: areaId1,
      //     city: areaId2,
      //     area: areaId3
      //   }
    })

  },
  //日期数值变动
  bindStartChange: function (e) {

    this.setData({
      dateStart: e.detail.value,
      ['form.startDate']: e.detail.value,
    })

  },

  bindEndChange: function (e) {
    this.setData({
      dateend: e.detail.value,
      ['form.endDate']: e.detail.value,
    })
  },


  //时间间隔数值变动
  bindSpaceChange: function (e) {
    var index = this.data.index
    this.setData({
      index: e.detail.value,
      ['form.time']: parseInt(e.detail.value) + 1
    })
  },








  //  点击时间组件确定事件  
  bindTimeChange: function (e) {
    this.setData({
      times: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getMessageList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this

    APP.getbackFrontInfo().then(res => {
      console.log(res.data.user);
      that.setData({
        userInfo: res.data.user
      })
    })



  

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    // 获取预约数组
    that.getMessageList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //列表信息
  getMessageList() {
    var list = {
      url: '/system/dept/queryList?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize,
      method: 'get',
      data: this.data.paramas
    }
    APP.request.backWxRequest(list).then(res => {
      if (res.data.code === 200) {
        var navList = this.data.navList
        var rows = res.data.rows
        var allArr = [...navList, ...rows]
        this.setData({
          navList: allArr
        })
      }
    });

  },

  xiugai(e) {
    console.log(e.currentTarget.dataset)
    this.setData({
      paramas: [],
      show: true,
      title: "修改信息",
      paramas: {
        deptId: e.currentTarget.dataset.deptid
      }
    })
    var personList = {
      url: '/system/dept/queryList',
      method: 'get',
      data: this.data.paramas
    }
    APP.request.backWxRequest(personList).then(res => {
      if (res.data.code === 200) {
        this.setData({
          form: res.data.rows[0],
          index: parseInt(res.data.rows[0].time) - 1,
          dateend: res.data.rows[0].endDate,
          dateStart: res.data.rows[0].startDate,
        });
        var province = {
          url: '/system/dept/queryProvince',
          method: 'get',
        }
        APP.request.backWxRequest(province).then(res => {

          if (res.data.code === 200) {
            this.setData({
              provincel: res.data.data
            });
          }
          for (var i = 0; i < this.data.provincel.length; i++)
            if (this.data.form.province == this.data.provincel[i].areaId) {
              this.setData({
                value5: this.data.provincel[i].name
              })
              console.log(1)
            }

          var cityL = {
            url: '/system/dept/queryCity?areaId=' + this.data.form.province,
            method: 'get',
          }
          APP.request.backWxRequest(cityL).then(res => {
            if (res.data.code === 200) {
              this.setData({
                cityL: res.data.data
              });
              for (var i = 0; i < this.data.cityL.length; i++)
                if (this.data.form.city == this.data.cityL[i].areaId)
                  this.setData({
                    value6: this.data.cityL[i].name
                  })
              var areal = {
                url: '/system/dept/queryArea?areaId=' + this.data.form.city,
                method: 'get',
              }
              APP.request.backWxRequest(areal).then(res => {
                if (res.data.code === 200) {
                  this.setData({
                    areal: res.data.data
                  });
                  for (var i = 0; i < this.data.areal.length; i++)
                    if (this.data.form.area == this.data.areal[i].areaId)
                      this.setData({
                        value7: this.data.areal[i].name
                      })

                }

              });
            }

          });
        });


      }
    });


  },

  shanchu(e) {
    let that = this
    wx.showModal({
      title: '警告',
      content: '确认删除吗',
      success(res) {

        if (res.confirm) {
          var shanchuList = {
            url: '/system/dept/deleteDept?deptId=' + e.currentTarget.dataset.deptid,
            method: 'get',
          }
          APP.request.backWxRequest(shanchuList).then(res => {
            if (res.data.code === 200) {
              that.setData({
  
                pageNum: 1,
                pageSize: 4,
                navList: []
              })
              that.getMessageList();
            }
          });
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }


     




      }
    })
  },

  tianjia() {
    this.setData({
      dateend: '',
      dateStart: '',
      paramas: [],
      form: [],
      show: true,
      title: "添加信息",
    })
  },
  //提交
  confirm() {
    // console.log(this.data.form.deptId)
    if (this.data.form.deptId != undefined) {
      if (!!!this.data.form.deptName) {
        wx.showToast({
          title: '请填写单位名称',
          image: '../../assets/error.png',
        })
      } else
      if (!!!this.data.form.phone) {
        wx.showToast({
          title: '请填写联系电话',
          image: '../../assets/error.png',
        })
      } else
      if (!!!this.data.form.address) {
        wx.showToast({
          title: '请填写地址',
          image: '../../assets/error.png',
        })
      } else
      if (!!!this.data.form.rooms) {
        wx.showToast({
          title: '请填写房间号',
          image: '../../assets/error.png',
        })
      } else {
        var personList = {
          url: '/system/dept/updateDept',
          method: 'post',
          data: this.data.form
        }
        APP.request.backWxRequest(personList).then(res => {
          if (res.data.code === 200) {
            this.setData({
              paramas: '',
              pageNum: 1,
              pageSize: 4,
              navList: []
            });
            this.getMessageList();
          }
        });
      }
    }
  },

})